﻿using System.Collections.Generic;
using System.Windows.Forms;
using LinkDownloader.Core;

namespace LinkDownloader.Forms.JobLists
{
    public class JobList : DataGridView
    {

        public const string ColumnNameLink = "clmLink";
        public const string ColumnNameSize = "clmSize";
        public const string ColumnNameStatus = "clmStatus";
        public const string ColumnNameProgress = "clmProgress";
        public const string ColumnNameTimeLeft = "clmTimeLeft";
        public const string ColumnNameTransferRate = "clmTransferRate";

        private Dictionary<string, int> columns = null;
        
        public JobList()
            : base()
        {
            this.InitializeComponent();
        }

        public void CompleteInitialization()
        {
            this.columns = new Dictionary<string, int>(this.Columns.Count);
            for (int i = 0; i < this.Columns.Count; i++)
            {
                this.columns.Add(this.Columns[i].Name, i);
            }
        }

        private void InitializeComponent()
        {
            
            this.SuspendLayout();
            
            this.Dock = System.Windows.Forms.DockStyle.Fill;
            this.Name = "grid";
            this.AllowUserToAddRows = false;
            this.AllowUserToDeleteRows = false;
            this.AllowUserToResizeRows = false;
            this.RowHeadersVisible = false;
            this.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.ShowCellErrors = false;
            this.ShowCellToolTips = false;
            this.ShowEditingIcon = false;
            this.ShowRowErrors = false;
                       
            this.ResumeLayout(false);
            
        }

        public int GetColumn(string colname)
        {
            int i;
            if (this.columns.TryGetValue(colname, out i)) { return i; }
            return -1;
        }

        public void IncrementProgress(object value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ChangeCallBack(IncrementProgress), value);
            }
            else
            {
                JobListRow row = (JobListRow)value;
                row.UpdateProgress();
            }
        }

        public void UpdateStatus(object value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ChangeCallBack(UpdateStatus), value);
            }
            else
            {
                JobListRow row = (JobListRow)value;
                row.UpdateStatus();
            }
        }

        public void UpdateSize(object value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ChangeCallBack(UpdateSize), value);
            }
            else
            {
                JobListRow row = (JobListRow)value;
                row.UpdateSize();
            }
        }

        public void UpdateTimeLeft(object value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ChangeCallBack(UpdateTimeLeft), value);
            }
            else
            {
                JobListRow row = (JobListRow)value;
                row.UpdateTimeLeft();
            }
        }

        public void UpdateTransferRate(object value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new ChangeCallBack(UpdateTransferRate), value);
            }
            else
            {
                JobListRow row = (JobListRow)value;
                row.UpdateTransferRate();
            }
        }

        protected override void OnCellContentDoubleClick(DataGridViewCellEventArgs e)
        {
            base.OnCellContentDoubleClick(e);
            if (e.RowIndex >= 0 && e.RowIndex < this.RowCount)
            {
                JobListRow row = this.Rows[e.RowIndex] as JobListRow;
                row.DownloadJob.Show();
            }
        }

    }
}
